package oop.ex3.common;

import java.io.IOException;
import java.net.InetAddress;

public class Protocol {

	public static final String MSG_END = "END";
	public static final String MSG_BEGIN = "BEGIN";
	public static final String MSG_WANT_FILE = "WANTFILE";
	public static final String MSG_WANT_SERVERS = "WANTSERVERS";
	public static final String MSG_START_SESSION = "BEGIN";
	public static final String MSG_FILE_ADDRESS = "FILEADDRESS";
	public static final String MSG_FILE_DOWNLOAD_BEGINS = "FILE";
	public static final String MSG_DONE = "DONE";
	public static final String MSG_WELCOME = "WELCOME";
	public static final String MSG_CONTAIN_FILE = "CONTAINFILE";
	public static final String MSG_DONT_CONTAIN_FILE = "DONTCONTAINFILE";
	public static final String MSG_END_LIST = "ENDLIST";
	public static final String MSG_CONTAIN_NS = "CONTAINNAMESERVER";
	public static final String MSG_END_SESSION = "ENDSESSION";
	public static final String MSG_FILE_NOT_FOUND = "FILENOTFOUND";
	public static final String MSG_GOODBYE = "GOODBYE";
	public static final String MSG_WANT_ALL_FILES = "WANTALLFILES";
	public static final String MSG_NO_KNOWN_FILES = "NOFILEEXIST";
	public static final String MSG_NS_CONTAINS_FILE = "NSCONTAINFILE";
	public static final String MSG_GO_AWAY = "GOAWAY";
	public static final String MSG_ERROR = "ERROR";
	
	
	public static void startSession(CommunicationSession session, int localPort) throws IOException {
		session.sendString(Protocol.MSG_BEGIN);
		session.sendString(InetAddress.getLocalHost().getHostAddress());
		session.sendInt(localPort);
		session.sendString(Protocol.MSG_END);

	}

	public static void endSession(CommunicationSession session) throws IOException {
		session.sendString(Protocol.MSG_END_SESSION);
		session.sendString(Protocol.MSG_END);

		// Reading DONE
		session.readString();

		// Reading END
		session.readString();
		session.close();
	}
}
